﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices.AccountManagement;

public partial class CheckUserInLDAP : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request["u"])) {
            LabelWarn.Text = "Please provide a username";
            
            return;
        }


        PrincipalContext ldapContext = new PrincipalContext(ContextType.Domain);
        UserPrincipal foundUser = UserPrincipal.FindByIdentity(ldapContext, Request["u"]);

        if (foundUser != null)
        {
            LabelDescription.Text = foundUser.Description;
            LabelEmail.Text = foundUser.EmailAddress;
            LabelEnabled.Text = foundUser.Enabled.ToString();
            LabelLastPasswordSet.Text = foundUser.LastPasswordSet != null ? foundUser.LastPasswordSet.Value.ToString("dd/MM/yyyy hh:mm:ss tt") : "-";
            LabelLastLogon.Text = foundUser.LastLogon != null ? foundUser.LastLogon.Value.ToString("dd/MM/yyyy hh:mm:ss tt") : "-";
            LabelAccountExpirationDate.Text = foundUser.AccountExpirationDate != null ? foundUser.AccountExpirationDate.ToString() : "-";
            LabelAccountLockoutTime.Text = foundUser.AccountLockoutTime != null ? foundUser.AccountLockoutTime.Value.ToString("dd/MM/yyyy hh:mm:ss tt") : "-";
            LabelPasswordNeverExpires.Text = foundUser.PasswordNeverExpires.ToString();
            
            LabelBadLogonCount.Text = foundUser.BadLogonCount.ToString();

            LabelWarn.Text = foundUser.Name + " " + foundUser.Surname + " [" + foundUser.UserPrincipalName + " ]";
        }
        else
        {
            LabelWarn.Text = "Utilizator inexistent in LDAP!";
        }
    }
}